package cn.iocoder.yudao.module.wine.service.supplylabel;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.supplylabel.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplylabel.SupplyLabelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplylabel.SupplyLabelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 供求标签管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyLabelServiceImpl implements SupplyLabelService {

    @Resource
    private SupplyLabelMapper supplyLabelMapper;

    @Override
    public Integer createSupplyLabel(SupplyLabelSaveReqVO createReqVO) {
        // 插入
        SupplyLabelDO supplyLabel = BeanUtils.toBean(createReqVO, SupplyLabelDO.class);
        supplyLabelMapper.insert(supplyLabel);
        // 返回
        return supplyLabel.getId();
    }

    @Override
    public void updateSupplyLabel(SupplyLabelSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyLabelExists(updateReqVO.getId());
        // 更新
        SupplyLabelDO updateObj = BeanUtils.toBean(updateReqVO, SupplyLabelDO.class);
        supplyLabelMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyLabel(Integer id) {
        // 校验存在
        validateSupplyLabelExists(id);
        // 删除
        supplyLabelMapper.deleteById(id);
    }

    private void validateSupplyLabelExists(Integer id) {
        if (supplyLabelMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SupplyLabelDO getSupplyLabel(Integer id) {
        return supplyLabelMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyLabelDO> getSupplyLabelPage(SupplyLabelPageReqVO pageReqVO) {
        return supplyLabelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplyLabelDO> selectLabelList() {
        return supplyLabelMapper.selectList();
    }

}
